package main;

import java.util.SortedSet;
import java.util.TreeSet;

/**
 * 
 * Representation of a node. Almost immutable.
 * 
 */
public class Node implements Comparable<Node> {

	/**
	 * Creates a new instance of the Node.
	 * 
	 * @param id
	 *            id of the node.
	 * @param capacity
	 *            capacity of the node.
	 */
	public Node(int id, int capacity) {
		this.id = id;
		this.capacity = capacity;
		this.block = null;
		this.preNodeSet = new TreeSet<Node>();
		this.postNodeSet = new TreeSet<Node>();
	}

	/**
	 * Id of the node. Immutable.
	 */
	private int id;
	/**
	 * Capacity of the node. Immutable.
	 */
	private int capacity;
	/**
	 * Block to which this node belongs. Guarded by this.
	 */
	private Block block;
	/**
	 * Set of predecessor nodes. Immutable.
	 */
	private SortedSet<Node> preNodeSet;
	/**
	 * Set of successor nodes. Immutable.
	 */
	private SortedSet<Node> postNodeSet;

	/**
	 * sets to which block this node belongs.
	 * 
	 * @param block
	 */
	public synchronized void setBlock(Block block) {
		this.block = block;
	}

	/**
	 * gets to which block this node belongs.
	 * 
	 * @return block.
	 */
	public synchronized Block getBlock() {
		return this.block;
	}

	/**
	 * gets the id of this node.
	 * 
	 * @return id of this node.
	 */
	public int getId() {
		return this.id;
	}

	/**
	 * gets the capacity of the node.
	 * 
	 * @return capacity
	 */
	public int getCapacity() {
		return this.capacity;
	}

	/**
	 * sets the capacity for this node.
	 * 
	 * @param capacity
	 */
	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

	/**
	 * adds predecessor of this node.
	 * 
	 * @param node
	 *            predecessor node.
	 */
	public void addPredNode(Node node) {
		this.preNodeSet.add(node);
	}

	/**
	 * adds successor of this node.
	 * 
	 * @param node
	 *            successor node.
	 */
	public void addPostNode(Node node) {
		this.postNodeSet.add(node);
	}

	/**
	 * gets the predecessor node set.
	 * 
	 * @return predecessor node set.
	 */
	public SortedSet<Node> getPredNodeSet() {
		return this.preNodeSet;
	}

	/**
	 * gets successor node set.
	 * 
	 * @return successor node set
	 */
	public SortedSet<Node> getPostNodeSet() {
		return this.postNodeSet;
	}

	/**
	 * String representation of this node.
	 */
	public String toString() {
		return this.id + "";
	}

	/**
	 * compares this nodes by id.
	 */
	@Override
	public int compareTo(Node node) {
		if (this.id < node.id)
			return -1;
		if (this.id > node.id)
			return 1;
		return 0;
	}

}
